﻿/** 表单扩展模块 date:2020-05-04   License By http://easyweb.vip */
layui.define(['form', 'xmSelect', 'laydate'], function (exports) {
    var $ = layui.jquery;
    var form = layui.form;
    var laydate = layui.laydate;
    var xmSelect = layui.xmSelect;
    var verifyText = {
        phoneX: '请输入正确的手机号',
        emailX: '邮箱格式不正确',
        urlX: '链接格式不正确',
        numberX: '只能填写数字',
        dateX: '日期格式不正确',
        identityX: '请输入正确的身份证号',
        psw: '密码必须5到12位，且不能出现空格',
        equalTo: '两次输入不一致',
        digits: '只能输入整数',
        digitsP: '只能输入正整数',
        digitsN: '只能输入负整数',
        digitsPZ: '只能输入正整数和0',
        digitsNZ: '只能输入负整数和0',
        minlength: '最少输入{minlength}个字符',
        maxlength: '最多输入{maxlength}个字符',
        min: '值不能小于{min}',
        max: '值不能大于{max}'
    };

    /** 扩展验证规则 */
    var verifyList = {
        /* 手机号 */
        phoneX: function (value, item) {
            var reg = /^1\d{10}$/;
            if (value && !reg.test(value)) {
                return verifyText.phoneX;
            }
        },
        /* 邮箱 */
        emailX: function (value, item) {
            var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (value && !reg.test(value)) {
                return verifyText.emailX;
            }
        },
        /* 网址 */
        urlX: function (value, item) {
            var reg = /(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/;
            if (value && !reg.test(value)) {
                return verifyText.urlX;
            }
        },
        /* 数字 */
        numberX: function (value, item) {
            if (value && isNaN(value)) {
                return verifyText.numberX;
            }
        },
        /* 日期 */
        dateX: function (value, item) {
            var reg = /^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/;
            if (value && !reg.test(value)) {
                return verifyText.dateX;
            }
        },
        /* 身份证 */
        identityX: function (value, item) {
            var reg = /(^\d{15}$)|(^\d{17}(x|X|\d)$)/;
            if (value && !reg.test(value)) {
                return verifyText.identityX;
            }
        },
        /* 密码 */
        psw: function (value, item) {
            if (value && !/^[\S]{5,12}$/.test(value)) {
                return verifyText.psw
            }
        },
        /* 重复 */
        equalTo: function (value, item) {
            if (value != $($(item).attr('lay-equalTo')).val()) {
                var text = $(item).attr('lay-equalToText');
                return text ? text : verifyText.equalTo;
            }
        },
        /* 整数 */
        digits: function (value, item) {
            var reg = /^-?\d+$/;
            if (value && !reg.test(value)) {
                return verifyText.digits;
            }
        },
        /* 正整数 */
        digitsP: function (value, item) {
            var reg = /^[1-9]\d*$/;
            if (value && !reg.test(value)) {
                return verifyText.digitsP;
            }
        },
        /* 负整数 */
        digitsN: function (value, item) {
            var reg = /^-[1-9]\d*$/;
            if (value && !reg.test(value)) {
                return verifyText.digitsN;
            }
        },
        /* 非负整数 */
        digitsPZ: function (value, item) {
            var reg = /^\d+$/;
            if (value && !reg.test(value)) {
                return verifyText.digitsPZ;
            }
        },
        /* 非正整数 */
        digitsNZ: function (value, item) {
            var reg = /^-[1-9]\d*|0/;
            if (value && !reg.test(value)) {
                return verifyText.digitsNZ;
            }
        },
        /* h5 */
        h5: function (value, item) {
            if (value) {
                var minlength = $(item).attr('minlength');
                var maxlength = $(item).attr('maxlength');
                var min = $(item).attr('min');
                var max = $(item).attr('max');
                if (minlength && value.length < minlength) {
                    return verifyText.minlength.replace(/{minlength}/g, minlength);
                }
                if (maxlength && value.length > maxlength) {
                    return verifyText.maxlength.replace(/{maxlength}/g, maxlength);
                }
                if (min && value * 1 < min * 1) {
                    return verifyText.min.replace(/{min}/g, min);
                }
                if (max && value * 1 > max * 1) {
                    return verifyText.max.replace(/{max}/g, max);
                }
            }
        }
    };

    var formX = {
        init: function () {
            form.verify(verifyList);
        },
        /* 赋值表单，解决top.layui.form.val无效的问题 */
        formVal: function (filter, object) {
            formX.val(filter, object);
        },
        /* 赋值表单，解决top.layui.form.val无效的问题 */
        val: function (filter, object) {
            $('.layui-form[lay-filter="' + filter + '"]').each(function () {
                var $item = $(this);
                for (var f in object) {
                    if (!object.hasOwnProperty(f)) continue;
                    var $elem = $item.find('[name="' + f + '"]');
                    if ($elem.length > 0) {
                        var type = $elem[0].type;
                        if (!type && $elem.length > 1) {
                            type = $elem[1].type
                        }
                        if (type === 'checkbox') {  // 如果为复选框
                            $elem[0].checked = object[f];
                        } else if (type === 'radio') { // 如果为单选框
                            $elem.each(function () {
                                if (this.value == object[f]) {
                                    this.checked = true;
                                }
                            });
                        } else { // 其它类型的表单
                            $elem.val(object[f]);
                        }
                    }
                }
            });
            form.render(null, filter);
        },
        /* 渲染select */
        renderSelect: function (param) {
            var defaultOption = {
                elem: undefined,
                data: [],
                name: undefined,
                value: undefined,
                hint: '请选择',
                initValue: undefined,
                method: 'get',
                where: undefined,
                headers: undefined,
                async: true,
                done: undefined,
                error: undefined
            };
            param = $.extend(defaultOption, param);
            if (typeof param.data === 'string') {
                $.ajax({
                    url: param.data,
                    type: param.method,
                    data: param.where,
                    dataType: 'json',
                    headers: param.header || param.headers,
                    async: param.async,
                    success: function (result, status, xhr) {
                        if (result.data) {
                            param.data = result.data;
                            formX.renderSelect(param);
                        } else {
                            param.error && param.error(xhr, result);
                        }
                    },
                    error: param.error
                });
            } else {
                var html = param.hint ? ('<option value="">' + param.hint + '</option>') : '';
                for (var i = 0; i < param.data.length; i++) {
                    if (param.name && param.value) {
                        html += ('<option value="' + param.data[i][param.value] + '"' + (param.data[i][param.value] == param.initValue ? ' selected' : '') + '>' + param.data[i][param.name] + '</option>');
                    } else {
                        html += ('<option value="' + param.data[i] + '"' + (param.data[i] == param.initValue ? ' selected' : '') + '>' + param.data[i] + '</option>');
                    }
                }
                $(param.elem).html(html);
                var $form = $(param.elem).parent('.layui-form');
                if ($form.length === 0) {
                    $form = $(param.elem).parentsUntil('.layui-form').last().parent();
                }
                form.render('select', $form.attr('lay-filter'));
                param.done && param.done(param.data);
            }
        },
        /*渲染多选select 配合使用xmselect*/
        renderMultiSelect:function (param) {
            var defaultOption = {
                el: undefined,
                data: [],
                name: 'select',
                value:undefined,
                prop:{value:'value'},
                hint: '请选择',
                initValue: undefined,
                filterable: true,
                direction: 'auto',
                toolbar: {
                    show: true,
                    list: ["ALL", "CLEAR", "REVERSE"]
                }
            };
            param = $.extend(defaultOption, param);
            if(param.value){
                param.prop.value = param.value;
            }
            let xmObj;
            xmObj = xmSelect.render(param);
            return xmObj;
        },
        /*渲染单选按钮*/
        renderRadio:function (param) {
            var defaultOption = {
                elem: undefined,
                data: [],
                name: undefined,
                value: undefined,
                title: undefined,
                initValue: undefined,
                defaultRadio: true
            };
            param = $.extend(defaultOption, param);
            var html = '';
            /*if (param.defaultRadio) {
                html = '<input type="radio" name="' + param.name + '" value="" title="全部">';
            }*/
            for (let i = 0; i < param.data.length; i++) {
                html += '<input type="radio" name="' + [param.name] + '" value="' + param.data[i][param.value] + '" title="' + param.data[i][param.title] + '" ' + (param.initValue && param.initValue == param.data[i][param.value] ? 'checked' : '') + 'lay-filter="'+param.filter+'" >';
            }
            $(param.elem).html(html);
            var $form = $(param.elem).parent('.layui-form');
            if ($form.length === 0) {
                $form = $(param.elem).parentsUntil('.layui-form').last().parent();
            }
            form.render('radio', $form.attr('lay-filter'));

        },
        /* 验证码倒计时 */
        startTimer: function (elem, time, format) {
            if (!time) time = 60;
            if (!format) {
                format = function (t) {
                    return t + 's';
                }
            }
            if (formX.timers[elem]) clearInterval(formX.timers[elem]);
            var orgHtml = $(elem).html();
            $(elem).html(format(time));
            $(elem).prop('disabled', true);
            $(elem).addClass('layui-btn-disabled');
            var timer = setInterval(function () {
                time--;
                if (time <= 0) {
                    clearInterval(timer);
                    $(elem).html(orgHtml);
                    $(elem).removeProp('disabled');
                    $(elem).removeClass('layui-btn-disabled');
                } else {
                    $(elem).html(format(time));
                }
            }, 1000);
            formX.timers[elem] = timer;
        },
        timers: {},
        /* 获取表单修改过的数据 */
        formUpdatedField: function (field, oldField) {
            if (typeof field == 'string') field = form.val(field);
            for (var key in field) {
                if (!field.hasOwnProperty(key)) continue;
                if (field[key] === oldField[key]) delete field[key];
            }
            if (Object.keys(field).length > 0) return field;
        },
        /*转换数组形式的时间*/
        toDateString:function (obj) {
            if(!obj || obj.length === 0){
                return '';
            }
            let str = '';
            for (let i = 0; i < obj.length; i++) {
                if(obj[i] < 10){
                    obj[i] = '0'+ obj[i];
                }
                if(i < 2){
                    str += obj[i] + '-';
                }else if(i === 2){
                    str += obj[i] + ' ';
                }else {
                    str += obj[i] + ':';
                }
            }
            return str.substring(0,str.length - 1);
        },
        /*渲染树形结构下拉选 配合使用xmselect*/
        renderTreeSelect:function (param) {
            var defaultOption = {
                el: undefined,
                autoRow: false,
                filterable: true,
                radio: true,
                clickClose: false,
                name: 'select',
                tree: {
                    show: true,
                    showFolderIcon: true,
                    showLine: true,
                    indent: 20,
                    expandedKeys: [-3],
                    strict: false,
                    clickExpand: true,
                    clickCheck: true
                },
                toolbar: {
                    show: true,
                    list: [ 'ALL', 'CLEAR', 'REVERSE' ]
                },
                data:undefined
            };
            param = $.extend(defaultOption, param);
            let xmObj;

            xmObj = xmSelect.render(param);
            return xmObj;
        },
        /**
         * 渲染多选树形组件
         * @param el 元素id
         * @param na 表单名称
         * @param da 数据
         * @param iss 是否遵循父子模式
         * @returns {*}
         */
        renderMultiTreeSelect:function (el,na,da,iss) {
            var defaultOption = {
                el: el,
                autoRow: false,
                filterable: true,
                name: na ? na : 'select',
                tree: {
                    show: true,
                    showFolderIcon: true,
                    showLine: true,
                    indent: 20,
                    expandedKeys: [-3],
                    strict: iss ? false : true,
                    clickExpand: true,
                    clickCheck: true
                },
                toolbar: {
                    show: true,
                    list: [ 'ALL', 'CLEAR', 'REVERSE' ]
                },
                data:da ? da : undefined
            };
            let xmObj;
            xmObj = xmSelect.render(defaultOption);
            return xmObj;
        },
        excelExport:function (url,method) {
            top.layer.alert("正在请求下载文档,请等待");
            if (!method) {
                method = 'GET';
            }
            let xhr = new XMLHttpRequest();
            let headers = getHeaders()[0];
            xhr.open(method, url, true);
            xhr.setRequestHeader(headers.name, headers.value);
            xhr.responseType = 'blob';
            xhr.onload = function (e) {
                let blob = this.response;
                console.log(blob)
                let filename = new Date().getTime() + ".xls";
                let a = document.createElement('a');

                blob.type = "application/octet-stream";
                let url = URL.createObjectURL(blob);

                a.href = url;
                a.download = filename;
                a.click();
                window.URL.revokeObjectURL(url);
            };
            xhr.send();
        },
        getCityPickerValue: function (elem) {
            let arr = [];
            $(elem).next().find('span.select-item').each(function () {
                arr.push($(this).data('code'));
            })
            return arr;
        },
        getCityPickerLastValue: function (elem) {
            let arr = [];
            $(elem).next().find('span.select-item').each(function () {
                arr.push($(this).data('code'));
            })
            return arr[arr.length - 1];
        },
        /**
         * 渲染日期插件
         * @param elem id或者name值
         */
        renderLaydate: function (elem) {
            let elemId = elem;
            if (!elem.indexOf('#') >= 0) {
                elemId = 'input[name="' + elem + '"]';
            }
            laydate.render({
                elem: elemId,
                type: 'date',
                range: false,
                trigger: 'click',
                theme: 'grid',
                btns: ['now', 'yesterday', 'beginMonth', 'clear', 'confirm'],
                ready: function (date) {
                    _this = this.elem;
                    let today = new Date();
                    let dayNumber = today.getDate();
                    $(".laydate-btns-yesterday").on('click', function () {
                        let format = new Date(today.getTime() - 1000 * 60 * 60 * 24).Format("yyyy-MM-dd");
                        _this.val(format);
                        $("#layui-laydate1").remove();
                    });

                    $(".laydate-btns-beginMonth").on('click', function () {
                        let format = new Date(today.getTime() - 1000 * 60 * 60 * 24 * (dayNumber - 1)).Format("yyyy-MM-dd");
                        _this.val(format);
                        $("#layui-laydate1").remove();
                    });
                }
            });
        },
        /**
         * 渲染日期范围选择插件
         * @param elem id或者name值
         */
        renderLaydateRange: function (elem) {
            let elemId = elem;
            if (!elem.indexOf('#') >= 0) {
                elemId = 'input[name="' + elem + '"]';
            }
            laydate.render({
                elem: elemId,
                type: 'date',
                range: true,
                trigger: 'click',
                theme: 'grid',
                btns: ['now', 'yesterday', 'nearly7day', 'nearly30day', 'week', 'month', 'lastMonth', 'year', 'lastYear', 'clear', 'confirm'],
                ready: function (date) {
                    _this = this.elem;
                    let today = new Date();
                    let dayNumber = today.getDate();
                    let monthNumber = today.getMonth() + 1;
                    let fullYear = today.getFullYear();
                    let weekDay = today.getDay();
                    let oneDayTime = 1000 * 60 * 60 * 24;
                    console.log(new Date(fullYear, monthNumber, 1))

                    $(".laydate-btns-yesterday").on('click', function () {
                        let format = new Date(today.getTime() - oneDayTime).Format("yyyy-MM-dd");
                        _this.val(format + ' - ' + format);
                        $("#layui-laydate1").remove();
                    });

                    $(".laydate-btns-nearly7day").on('click', function () {
                        let format = new Date(today.getTime() - oneDayTime).Format("yyyy-MM-dd");
                        let format1 = new Date(today.getTime() - oneDayTime * 7).Format("yyyy-MM-dd");
                        _this.val(format1 + ' - ' + format);
                        $("#layui-laydate1").remove();
                    });

                    $(".laydate-btns-week").on('click', function () {
                        let format = new Date(today.getTime() + oneDayTime * 3).Format("yyyy-MM-dd");
                        let format1 = new Date(today.getTime() - oneDayTime * (weekDay - 1)).Format("yyyy-MM-dd");
                        _this.val(format1 + ' - ' + format);
                        $("#layui-laydate1").remove();
                    });

                    $(".laydate-btns-nearly30day").on('click', function () {
                        let format = new Date(today.getTime() - oneDayTime).Format("yyyy-MM-dd");
                        let format1 = new Date(today.getTime() - oneDayTime * 30).Format("yyyy-MM-dd");
                        _this.val(format1 + ' - ' + format);
                        $("#layui-laydate1").remove();
                    });
                    $(".laydate-btns-month").on('click', function () {
                        let format = new Date(today.getTime() - oneDayTime * (dayNumber - 1)).Format("yyyy-MM-dd");
                        let format1 = new Date(new Date(fullYear, monthNumber, 1).getTime() - oneDayTime).Format("yyyy-MM-dd");
                        _this.val(format + ' - ' + format1);
                        $("#layui-laydate1").remove();
                    });

                    $(".laydate-btns-lastMonth").on('click', function () {
                        let format = new Date(today.getTime() - oneDayTime * (dayNumber)).Format("yyyy-MM-dd");
                        let format1 = new Date(new Date(fullYear, monthNumber - 2, 1).getTime()).Format("yyyy-MM-dd");
                        _this.val(format1 + ' - ' + format);
                        $("#layui-laydate1").remove();
                    });

                    $(".laydate-btns-year").on('click', function () {
                        let format = new Date(new Date(fullYear, 0, 1).getTime()).Format("yyyy-MM-dd");
                        let format1 = new Date(new Date(fullYear + 1, 0, 1).getTime() - oneDayTime).Format("yyyy-MM-dd");
                        _this.val(format + ' - ' + format1);
                        $("#layui-laydate1").remove();
                    });

                    $(".laydate-btns-lastYear").on('click', function () {
                        let format = new Date(new Date(fullYear - 1, 0, 1).getTime()).Format("yyyy-MM-dd");
                        let format1 = new Date(new Date(fullYear, 0, 1).getTime() - oneDayTime).Format("yyyy-MM-dd");
                        _this.val(format + ' - ' + format1);
                        $("#layui-laydate1").remove();
                    });


                }
            });
        },
        /**
         * 封装对象集合为树形结构数据
         */
        initTreeData: function (data, name, value, pid) {
            if (!data && data.length === 0) {
                return data;
            }
            let res = [];
            for (let i = 0; i < data.length; i++) {
                let datapid = data[i][pid];
                if (!datapid || datapid == 0) {
                    res.push(data[i]);
                    break;
                }
                for (let j = 0; j < data.length; j++) {
                    let datavalue = data[j][value];
                    if (datapid == datavalue) {
                        data[j].children.push(data[i]);
                        break;
                    }
                }
            }
            return res;
        }
    };


    //js 时间格式化
    Date.prototype.Format = function (fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    formX.init();
    exports('formX', formX);
});
